<template>
  <el-dialog
    :title="headTitle"
    :visible.sync="dialogVisible"
    width="400px"
    :before-close="handleClose"
    :close-on-click-modal="false"
  >
    <div class="dialog">
      <div class="dialog-list">
        <label require="true">账户名:</label>
        <div class="span">
          <el-input size="small" v-model="formData.name"></el-input>
        </div>
      </div>
      <div class="dialog-list">
        <label require="true">账户类型:</label>
        <div class="span">
          <el-select size="small" v-model="formData.infos" placeholder="请选择">
            <el-option
              v-for="item in treeData"
              :key="item.id"
              :label="item.name"
              :value="JSON.stringify(item)"
            >
            </el-option>
          </el-select>
        </div>
      </div>
      <div class="dialog-list">
        <label>账户资产:</label>
        <div class="span">
          <el-input size="small" v-model="formData.accountAmount"></el-input>
        </div>
      </div>
      <div class="dialog-list">
        <label></label>
        <div class="span">
          {{ numberToUpper(formData.accountAmount) }}
        </div>
      </div>
      <div class="dialog-list">
        <label>编码:</label>
        <div class="span">
          <el-input size="small" v-model="formData.code"></el-input>
        </div>
      </div>

      <div class="dialog-list">
        <label>启用:</label>
        <div class="span">
          <el-switch
            size="small"
            v-model="formData.status"
            active-color="#13ce66"
            inactive-color="#ccc"
            active-value="0"
            inactive-value="1"
          >
          </el-switch>
        </div>
      </div>
      <div class="dialog-list" v-if="isShow">
        <label>所属组织:</label>
        <div class="span">
          <el-select
            size="small"
            v-model="formData.belongCompany"
            placeholder="请选择"
          >
            <el-option
              v-for="item in levelOneDeptList"
              :key="item.deptId"
              :label="item.deptName"
              :value="item.deptId"
            >
            </el-option>
          </el-select>
        </div>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="save">提交</el-button>
      <el-button @click="(dialogVisible = false), updateKey()">取 消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import request from "@/utils/request";
import Qs from "qs";
export default {
  props: {
    headTitle: {},
    treeData: {}
  },
  data() {
    return {
      dialogVisible: false,
      dateRange: "",
      formData: {
        name: "",
        code: "",
        status: "1",
        accountAmount: "",
        infos: "",
        belongCompany:'',
      },
      value: "",
      pData: {},
      levelOneDeptList: [],
      isShow: true
    };
  },
  methods: {
    save() {
      if (!this.formData.name || this.formData.name === "") {
        this.$message.error("账户名不能为空");
        return;
      }
      if (!this.formData.infos || this.formData.infos === "") {
        this.$message.error("类型不能为空");
        return;
      }
      this[this.pData.type]();
    },
    numberToUpper(money) {
      let prefix = "";
      if (money < 0) {
        prefix = "负";
        money=Math.abs(money)
      }
      var cnNums = new Array(
        "零",
        "壹",
        "贰",
        "叁",
        "肆",
        "伍",
        "陆",
        "柒",
        "捌",
        "玖"
      ); //汉字的数字
      var cnIntRadice = new Array("", "拾", "佰", "仟"); //基本单位
      var cnIntUnits = new Array("", "万", "亿", "兆"); //对应整数部分扩展单位
      var cnDecUnits = new Array("角", "分", "毫", "厘"); //对应小数部分单位
      var cnInteger = "整"; //整数金额时后面跟的字符
      var cnIntLast = "元"; //整型完以后的单位
      var maxNum = 999999999999999.9999; //最大处理的数字
      var IntegerNum; //金额整数部分
      var DecimalNum; //金额小数部分
      var ChineseStr = ""; //输出的中文金额字符串
      var parts; //分离金额后用的数组，预定义
      if (money == "") {
        return "";
      }
      money = parseFloat(money);
      if (money >= maxNum) {
        alert("超出最大处理数字");
        return "";
      }
      if (money == 0) {
        ChineseStr = cnNums[0] + cnIntLast + cnInteger;
        return ChineseStr;
      }
      money = money.toString(); //转换为字符串
      if (money.indexOf(".") == -1) {
        IntegerNum = money;
        DecimalNum = "";
      } else {
        parts = money.split(".");
        IntegerNum = parts[0];
        DecimalNum = parts[1].substr(0, 4);
      }
      if (parseInt(IntegerNum, 10) > 0) {
        //获取整型部分转换
        var zeroCount = 0;
        var IntLen = IntegerNum.length;
        for (var i = 0; i < IntLen; i++) {
          var n = IntegerNum.substr(i, 1);
          var p = IntLen - i - 1;
          var q = p / 4;
          var m = p % 4;
          if (n == "0") {
            zeroCount++;
          } else {
            if (zeroCount > 0) {
              ChineseStr += cnNums[0];
            }
            zeroCount = 0; //归零
            ChineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
          }
          if (m == 0 && zeroCount < 4) {
            ChineseStr += cnIntUnits[q];
          }
        }
        ChineseStr += cnIntLast;
        //整型部分处理完毕
      }
      if (DecimalNum != "") {
        //小数部分
        var decLen = DecimalNum.length;
        for (var i = 0; i < decLen; i++) {
          var n = DecimalNum.substr(i, 1);
          if (n != "0") {
            ChineseStr += cnNums[Number(n)] + cnDecUnits[i];
          }
        }
      }
      if (ChineseStr == "") {
        ChineseStr += cnNums[0] + cnIntLast + cnInteger;
      } else if (DecimalNum == "") {
        ChineseStr += cnInteger;
      }
      return `${prefix}${ChineseStr}`;
    },
    add() {
      request({
        url: "/system/accountTag/add",
        method: "post",
        data: {
          name: this.formData.name,
          code: this.formData.code,
          accountAmount: this.formData.accountAmount,
          status: this.formData.status,
          parentId: JSON.parse(this.formData.infos).id,
          type: JSON.parse(this.formData.infos).type,
          category: JSON.parse(this.formData.infos).category,
          belongCompany: this.formData.belongCompany
        }
      }).then(res => {
        this.$emit("reload");
        this.handleClose();
        console.log(res);
      });
    },
    edit() {
      request({
        url: "/system/accountTag/edit",
        method: "post",
        data: {
          id: this.pData.selectData.id,
          name: this.formData.name,
          code: this.formData.code,
          accountAmount: this.formData.accountAmount,
          status: this.formData.status,
          parentId: JSON.parse(this.formData.infos).id,
          type: JSON.parse(this.formData.infos).type,
          category: JSON.parse(this.formData.infos).category,
          belongCompany: this.formData.belongCompany
        }
      }).then(res => {
        this.$emit("reload");
        this.handleClose();
        console.log(res);
      });
    },
    open(data) {
      console.log(data);
      this.dialogVisible = true;
      this.levelOneDept();
      this.pData = data;
      if (data.type === "edit") {
        const infos = this.treeData.filter(
          item => item.id === data.selectData.parentId
        )[0];
        this.formData.name = data.selectData.name;
        this.formData.code = data.selectData.code;
        this.formData.accountAmount = data.selectData.accountAmount;
        this.formData.status = data.selectData.status;
        this.formData.infos = JSON.stringify({ ...infos });
        
        if (this.formData.parentId === 108 || this.formData.id === 108) {
          this.isShow = false;
          
        }else{
          this.formData.belongCompany = data.selectData.belongCompany||''
        }
      }
      
      
    },
    levelOneDept() {
      return new Promise((resolve,reject)=>{
        request({
        url: "/system/accountTag/levelOneDept",
        method: "post"
      }).then(res => {
        this.levelOneDeptList = res.rows || [];
        console.log(res);
        resolve(res)
      });
      })
    },
    updateKey() {
      this.$emit("update:keyIndex", new Date().getTime());
    },
    handleClose(done) {
      this.dialogVisible = false;
      this.updateKey();
    }
  }
};
</script>

<style lang="scss" scoped>
.el-dialog__wrapper {
  .dialog {
    width: 100%;

    &-list {
      width: 100%;
      height: 40px;
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      align-items: center;
      margin: 10px 0;

      label {
        width: 100px;
        text-align: right;
        padding: 0 10px;
      }

      label[require="true"]::before {
        content: "*";
        color: red;
      }

      .span {
        width: calc(100% - 100px);
        height: 100%;
        display: flex;
        align-items: center;

        .el-select,
        .el-date-editor {
          width: 100% !important;
        }
      }
    }
  }
}
</style>
